import { getuser, userdelete, userupdate, userroles } from '@/api/user'
import { getMenuRoutesByRole } from "@/api/common"
export default {
    namespaced: true,
    state: {
        list: [],
        total: 0,
        page: 1,
        pageSize: 20,
        inp: '',
        prop: 'asc',
        order: 'id',
        accessroutes: []
    },
    mutations: {
        setuser(state, res) {

            state.list = res.data
            console.log(state.list)
            state.total = res.total
        },
        currentpage(state, page) {
            state.page = page.page;
            if (page.isAdd == 0) {
                this.dispatch('user/getUser', 0);
                state.page = 1
            } else {
                this.dispatch('user/getUser', 1);
                state.page = 1
            }
        },
        inpchange(state, inp) {
            state.inp = inp.inp;
            if (inp.isAdd == 0) {
                this.dispatch('user/getUser', 0);
                state.inp = ''
            } else {
                this.dispatch('user/getUser', 1);
                state.inp = ''
            }
        },
        sortAge(state, sort) {
            if (sort.isAdd == 0) {
                if (sort.sort.order == 'ascending') {
                    state.order = 'age'
                    state.prop = 'asc'
                } else if (sort.sort.order == 'descending') {
                    state.order = 'age'
                    state.prop = 'desc'
                } else {
                    state.order = 'id'
                    state.prop = 'asc'
                }
                this.dispatch('user/getuser', 0);
            } else {
                if (sort.sort.order == 'ascending') {
                    state.order = 'age'
                    state.prop = 'asc'
                } else if (sort.sort.order == 'descending') {
                    state.order = 'age'
                    state.prop = 'desc'
                } else {
                    state.order = 'id'
                    state.prop = 'asc'
                }
                this.dispatch('user/getuser', 1);
            }
        },
        SET_ACCESSROUTES(state, accessroutes){
            state.accessroutes = accessroutes;
        }

    },

    actions: {
        async deleteusers({ commit }, id) {
            const res = await userdelete({ id: id });
        },
        async getUser({ commit, state }, status) {
            const res = await getuser({ order: state.order, prop: state.prop, status: status, keyword: state.inp, page: state.page - 1, pageSize: state.pageSize })
            commit('setuser', res.data)
        },
        async updateuser({ commit }, status) {
            const res = await userupdate(status);
        },
        async setroles({commit}, {menu, items}){
            const filterMenu = getMenuRoutesByRole(menu, items)
            console.log(filterMenu);
            commit('SET_ACCESSROUTES', filterMenu)
        }

    },
    getters: {}
}